package cn.tedu._05interceptor.base.config;

import cn.tedu._05interceptor.base.interceptors.MyInterceptor1;
import cn.tedu._05interceptor.base.interceptors.MyInterceptor2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    //addI+回车

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册拦截器,并添加拦截策略: **是拦截器中的通配符,代表匹配任意路径;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/**");

        //1.拦截所有请求:通配符 ** ;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/**");

        //2.拦截指定的多个请求,拦截登录和注册功能;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/user/reg", "/v1/user/login");

        // 拦截用户模块的所有请求
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/user/**");

        //3.排除指定的多个请求;
        //  拦截用户模块中,除了登录之外的所有请求;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/user/**")
        //        .excludePathPatterns("/v1/user/login");

        /*
            preHandle: [pre1, pre2]
            postHandle: [post1, post2]
            afterCompletion: [after1, after2]
         */
        registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/v1/**");
        registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/v1/**");
    }
}
